package com.ruoyi.common.config;

import com.alipay.api.DefaultAlipayClient;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Component
public class AliConfig {
    /**
     *初始化SDK
     */
    public static DefaultAlipayClient alipayClient;
    /**
     * 支付宝开放平台url
     */
    public static String aliPayURL = "https://openapi.alipay.com/gateway.do";
    /**支付宝开放平台应用ID*/
    public static String appId = "2021004123693256";
    /**支付宝开放平台应用私钥*/
    public static String privateKey ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCfp2yjjyDI4GIAzGDJyCQpsunlAjazrKBAgS+mcOLbHYuxTb4UR7mRUU/uMHeeW1mRo8UyoCAP3i9sQywk7tD0bCRfduiQLrEeEO9x/azqG/5ey34yy1JdnuzDJyMGi22NH3XPguckumSEnjlZhPMJdYD9Ys9tZ65HyEMBcTwjLtBMlPnHjHMC2ESNt9NJUjH79CPdspVNKc99du3C2EdtiygVnUtQyGhxJ5Aw2ZuOZ1yaHySSzMgu0072RtYCBuMY2Sh7Bqu+bJ3LuoIpM6lEpJJhqg5RI5dTefhIQUI0xSf01o6yv5mRXaBdfNgdWR46wV5GHvKA0xrO31KysdAJAgMBAAECggEAfT/FlNL2b2DIwcOUsvhWlngL0mNYMsDcv34ZucOZ1ThqnOBWnGZgiEZ4JG7ViOA1dnI6hCrmvEwcfBx+h0LrnrUwG8yXSnW28dVrY8wVWVw4xHl0ObkUlunU6IzNh5bejPX/YJpbZ47qyLewWjBY0jTiyuY3wKa0LRBIsNr6yMX07JE9KZtVyTHM4P1HQ4hAzQm2ripTNbVpIxOPhGkG3ui6JGyqPZP99SZVXZyx9fPYYH08T1sRWkPH4Nv/xJNoR8pofB7Q3KiWu68Chl8n6LUMQDvyn7PU958fZdJXr2NgDoym2eHReuEcFx9gasfSvbo8+S3vDzuUPLrLzro94QKBgQDajT7q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wVL0YYNGy+N6LrX5fVKsZ1tK+zG+0X/q53LEX2Ao0xityqIKIV7kk9BIF2eNtaSTEmXZOopNNZsntrMfyu7efR33NmXLLJkT10z0/hts9nPLihePISDjXjY72kIjLBQCHZvuwa4ey/8jvSt/3lIPCCNnjszDNcSUmXFeiuaaMEMkeTLjV673gU9CO1RqnQ1AQ6E1a1KK4zCUmcnCQIDAQAB";
    /**授权回调地址*/
    public static String notifyUrl = "your_notify_uri";
    /**
     *用授权码来换取授权令牌
     */
    public static String grantType = "authorization_code";

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    private static final Random random = new Random();

    /**
     * 生成订单号
     * @param prefix
     * @return
     */
    public static String generateOrderNumber(String prefix) {
        // 获取当前时间戳
        LocalDateTime now = LocalDateTime.now();
        String timestamp = now.format(formatter);

        // 生成四位随机数
        int randomNumber = random.nextInt(1000);
        String randomNumFormatted = String.format("%04d", randomNumber);

        // 生成唯一订单号
        return prefix + timestamp + randomNumFormatted;
    }

    /**
     * 设置签名
     */
    public static List<String> sign(Map<String, Object> map, String signKey){
        if (map==null)
            return null;
        if (map.containsKey("sign")){
            map.remove("sign");
        }
        ArrayList<String> keyList = new ArrayList<>(map.keySet());
        Collections.sort(keyList);
        StringBuffer stringBuffer = new StringBuffer();
        for(int i=0;i<keyList.size();i++){
            String key = keyList.get(i);
            Object value = map.get(key);
            stringBuffer.append(key+"="+value+"&");
        }
        String signStr = (stringBuffer.substring(0,stringBuffer.length()-1)+signKey).toLowerCase();
        System.out.println("before sign: "+signStr);
        String md5Str = "sign="+MD5(signStr, "utf-8");
        System.out.println("after sign: "+md5Str);
        ArrayList<String> signList = new ArrayList<>();
        signList.add(0,signStr);
        signList.add(1,md5Str);
        return signList;
    }

    /**
     * 生成MD5
     * @param s
     * @return
     */
    private static String MD5( String s) {
        return MD5(s,"utf-8");
    }

    /**
     * 生成md5
     * @param s
     * @param charset
     * @return
     */
    public static String MD5(String s, String charset){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            byte digest[] = messageDigest.digest(s.getBytes(charset));
            return byteToString(digest);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    private static String byteToString(byte digest[]){
        int i = digest.length;
        char chars[] = new char[i * 2];
        int j=0;
        for (int k=0;k<i;k++){
            byte b = digest[k];
            chars[j++]=hexDigits[b >>> 4 & 0xf];
            chars[j++]=hexDigits[b & 0xf];
        }
        return new String(chars);
    }

    private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    //停车接口
    public static String ParkingApi = "http://service.xiquewl.com/api/alipay/parkingInfo.asp";

    //请求计费接口
    public static String ParkingExpenses = "http://s.appykt.com/zld/parkapi/query_price";

}
